package com.lmuse.xmr.foodspotting.db;

import com.lmuse.xmr.foodspotting.model.Follower;
import com.lmuse.xmr.foodspotting.model.User;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.datatype.BmobPointer;
import cn.bmob.v3.datatype.BmobRelation;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.UpdateListener;

public class FollowPresenterImpl implements Contract.FollowPresenter {

    private Contract.FollowView followView;

    public FollowPresenterImpl(Contract.FollowView followView) {
        this.followView = followView;
    }

    @Override
    public void addFollowing(final String followingId) {
        final User me = BmobUser.getCurrentUser(User.class);
        BmobRelation relation = new BmobRelation();
        final User user = new User();
        user.setObjectId(followingId);
        relation.add(user);
        me.setFollowings(relation);
        me.update(new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if (e == null) { //添加关注关系成功
                    followView.onAddFollowingSuccess();
                    updateFollowerd(followingId, "add");
                }else {
                    followView.onAddFollowingFailed(e.toString());
                }
            }
        });
    }

    @Override
    public void unFollowing(final String followingId) {
        final User me = BmobUser.getCurrentUser(User.class);
        final User user = new User();
        user.setObjectId(followingId);
        BmobRelation relation = new BmobRelation();
        relation.remove(user);
        me.setFollowings(relation);
        me.update(new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if (e == null) {//删除关注用户成功
                    followView.onUnFollowingSuccess();
                    updateFollowerd(followingId, "subtract");
                }else {
                    followView.onUnFollowingFailed(e.toString());
                }
            }
        });
    }

    /**
     * 更新粉丝数
     * @param userId
     * @param way 加或减
     */
    void updateFollowerd(final String userId, final String way) {
        BmobQuery<Follower> query = new BmobQuery<Follower>();
        //查询playerName叫“比目”的数据
        query.addWhereEqualTo("userId", userId);
        //返回50条数据，如果不加上这条语句，默认返回10条数据
        query.setLimit(1);
        //执行查询方法
        query.findObjects(new FindListener<Follower>() {
            @Override
            public void done(List<Follower> object, BmobException e) {
                if(e == null){
                    Integer num = object.get(0).getFollowers();
                    Follower follower = new Follower();
                    if (way.equals("add")) {
                        num++;
                        follower.setFollowers(num);
                    }else {
                        num--;
                        follower.setFollowers(num);
                    }
                    follower.update(object.get(0).getObjectId(), new UpdateListener() {
                        @Override
                        public void done(BmobException e) {
                            if (e != null) {
                                updateFollowerd(userId, way);
                            }
                        }
                    });
                }else{
                    updateFollowerd(userId, way);
                }
            }
        });
    }

    @Override
    public void queryFollowings() {
        BmobQuery<User> query = new BmobQuery<>();
        User me = BmobUser.getCurrentUser(User.class);
        query.addWhereRelatedTo("followings", new BmobPointer(me));
        query.findObjects(new FindListener<User>() {
            @Override
            public void done(List<User> list, BmobException e) {
                if (e == null) {
                    followView.onQueryFollowingSuccess(list);
                }else {
                    followView.onQueryFollowingFailed(e.toString());
                }
            }
        });
    }

    @Override
    public void queryFollowers() {
        BmobQuery<User> query = new BmobQuery<>();
        User me = BmobUser.getCurrentUser(User.class);
        query.addWhereRelatedTo("follower", new BmobPointer(me));
        query.findObjects(new FindListener<User>() {
            @Override
            public void done(List<User> list, BmobException e) {
                if (e == null) {
                    followView.onQueryFollowerSuccess(list);
                }else {
                    followView.onQueryFollowerFailed(e.toString());
                }
            }
        });
    }

    @Override
    public void queryMyFollowersAmount() {
        BmobQuery<Follower> query = new BmobQuery<Follower>();
        User user = BmobUser.getCurrentUser(User.class);
        //查询playerName叫“比目”的数据
        query.addWhereEqualTo("userId", user.getObjectId());
        //返回50条数据，如果不加上这条语句，默认返回10条数据
        query.setLimit(1);
        //执行查询方法
        query.findObjects(new FindListener<Follower>() {
            @Override
            public void done(List<Follower> object, BmobException e) {
                if(e==null){
                    Integer num = object.get(0).getFollowers();
                    followView.onQueryFollowerAmountSuccess(num);
                }else{
                    followView.onQueryFollowerAmountFailed(e.toString());
                }
            }
        });
    }

    @Override
    public void onDetach() {

    }
}
